package com.jsmtr.rms.controller.UserResumeView;

import com.jsmtr.rms.entity.*;
import com.jsmtr.rms.service.IDoResumeService;
import com.jsmtr.rms.service.IProjectService;
import com.jsmtr.rms.service.IResumeService;
import com.jsmtr.rms.service.IUtilService;
import com.jsmtr.rms.utils.DateTools;
import com.jsmtr.rms.utils.EnumTools;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 查看应聘者简历控制
 *
 * @author: cyx
 * @description:
 * @date: Created in 2019/2/15 11:51
 * @modified by:
 */
@Controller
public class UserResumeViewController {
    @Resource
    private IDoResumeService doResumeService;
    @Resource
    private IResumeService resumeService;
    @Resource
    private IUtilService utilService;
    @Resource
    private IProjectService projectService;

    //region 查看投递的简历
    @RequestMapping("/UserResume/ViewDResume")
    public String View(HttpServletRequest request, Model model) {
        try {
            String id = request.getParameter("id").trim();
            DUserResume duserResume = doResumeService.getDUserResume(id);
            UserResume resume = doResumeService.gUserResumeByUid(duserResume.getUserid());
            if (resume == null) {
                model.addAttribute("valid", "no");
            } else {
                ProjectInfoJobs projectInfoJobs = projectService.getProjectInfoJob(duserResume.getJid());
                model.addAttribute("valid", "yes");
                model.addAttribute("id", id);
                model.addAttribute("fpath", utilService.getConfig("path"));
                model.addAttribute("pathfile", "/resources/ResumeAppends" + resume.getName() + resume.getCardno());
                model.addAttribute("isornot", EnumTools.IsOrNotList());
                model.addAttribute("proname", duserResume.getProname());
                model.addAttribute("proid", duserResume.getProid());
                model.addAttribute("fid", duserResume.getFid());
                model.addAttribute("jname", duserResume.getJname());
                model.addAttribute("jobdepart", projectInfoJobs.getJobdepart());
                model.addAttribute("jobname", projectInfoJobs.getJobname());
                model.addAttribute("code", duserResume.getCode());
                String jtime = DateTools.toDayStr(duserResume.getAddDate());
                model.addAttribute("jobtime", jtime);
                String rid = duserResume.getId();

                List<DUserWorkExperience> workexperiences = doResumeService.getUserWorkExperience(rid); //工作经验
                List<DUserTraining> trainings = doResumeService.getUserTraining(rid); //培训经历
                List<DUserRewardPunishment> rewardpunishments = doResumeService.getUserRewardPunishment(rid); //获奖信息
                List<DUserFamily> families = doResumeService.getUserFamily(rid);//家庭成员
                List<DUserMajor> majorList = doResumeService.getUserMajor(rid); //专业技术职称
                if (majorList == null || majorList.size() <= 0) {
                    List<UserMajor> userMajorList = resumeService.getMajors(resume.getId());
                    model.addAttribute("majors", userMajorList);
                }
                List<UserPaper> userPapers = doResumeService.getUserPaper(rid); //发表论文
                List<UserAppend> userAppends = doResumeService.getUserAppend(duserResume.getCardno()); //证明材料

                List<UserAppend> userAppendRys = new ArrayList<>();//荣誉
                List<UserAppend> userAppendJxs = new ArrayList<>();//奖项
                List<UserAppend> userAppendZcs = new ArrayList<>();//职称
                List<UserAppend> userAppendOts = new ArrayList<>();//其他

                for (UserAppend userAppend : userAppends) {
                    if (userAppend.getType().equals("G")) {
                        userAppend.setRealname("获得的荣誉" + (userAppendRys.size() > 0 ? "(" + userAppendRys.size() + ")" : ""));
                        userAppendRys.add(userAppend);
                    } else if (userAppend.getType().equals("1")) {
                        userAppend.setRealname("学校期间所获奖项" + (userAppendJxs.size() > 0 ? "(" + userAppendJxs.size() + ")" : ""));
                        userAppendJxs.add(userAppend);
                    } else if (userAppend.getType().equals("4")) {
                        userAppend.setRealname("职称或职业资格" + (userAppendZcs.size() > 0 ? "(" + userAppendZcs.size() + ")" : ""));
                        userAppendZcs.add(userAppend);
                    } else if (userAppend.getType().equals("A")) {
                        userAppend.setRealname("身份证正面");
                        userAppendOts.add(userAppend);
                    } else if (userAppend.getType().equals("B")) {
                        userAppend.setRealname("身份证反面");
                        userAppendOts.add(userAppend);
                    } else if (userAppend.getType().equals("C")) {
                        userAppend.setRealname("全日制教育毕业证");
                        userAppendOts.add(userAppend);
                    } else if (userAppend.getType().equals("D")) {
                        userAppend.setRealname("全日制教育学位证");
                        userAppendOts.add(userAppend);
                    } else if (userAppend.getType().equals("E")) {
                        userAppend.setRealname("非全日制教育毕业证");
                        userAppendOts.add(userAppend);
                    } else if (userAppend.getType().equals("F")) {
                        userAppend.setRealname("非全日制教育学位证");
                        userAppendOts.add(userAppend);
                    }
                }
                model.addAttribute("userAppendRys", userAppendRys);
                model.addAttribute("userAppendJxs", userAppendJxs);
                model.addAttribute("userAppendZcs", userAppendZcs);
                model.addAttribute("userAppendOts", userAppendOts);

                model.addAttribute("userresume", resume);
                model.addAttribute("workexperiences", workexperiences);
                model.addAttribute("workLen", workexperiences.size() + 1);
                model.addAttribute("trainings", trainings);
                model.addAttribute("rewardpunishments", rewardpunishments);
                model.addAttribute("families", families);
                model.addAttribute("famLen", families.size() < 2 ? 2 : (families.size() + 1));
                model.addAttribute("famL", families.size());

                model.addAttribute("papers", userPapers);

                /*
                List<DUserEducation> educations = doResumeService.getUserEducation(rid);//教育背景
                List<DUserEducation> educationsFull = new ArrayList<>();
                List<DUserEducation> educationsNot = new ArrayList<>();
                for(DUserEducation dUserEducation : educations){
                    if(dUserEducation.getFulltime().equals("0"))
                        educationsFull.add(dUserEducation);
                    else
                        educationsNot.add(dUserEducation);
                }*/
                List<UserEducation> educations = resumeService.getUserEducation(resume.getId());//教育背景
                List<UserEducation> educationsFull = new ArrayList<>();
                List<UserEducation> educationsNot = new ArrayList<>();
                for (UserEducation dUserEducation : educations) {
                    if (dUserEducation.getType().equals(1))
                        educationsFull.add(dUserEducation);
                    else
                        educationsNot.add(dUserEducation);
                }
                model.addAttribute("educations", educations);
                model.addAttribute("educationsFull", educationsFull);
                model.addAttribute("eduFullLen", educationsFull.size() < 2 ? 2 : (educationsFull.size() + 1));
                model.addAttribute("eduFullL", educationsFull.size());
                model.addAttribute("educationsNot", educationsNot);
                model.addAttribute("eduNotLen", educationsNot.size() < 2 ? 2 : (educationsNot.size() + 1));
                model.addAttribute("eduNotL", educationsNot.size());

                Date dates = new Date();
                Date brith = resume.getBirthday();
                String ages = getTime(dates, brith);
                model.addAttribute("ages", ages);

                if (duserResume.getProid().indexOf("A_") >= 0)//社招
                    model.addAttribute("social", "social");

                if (duserResume.getChangeDate() != null
                        && duserResume.getChangeDate().after(duserResume.getAddDate())) {
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    model.addAttribute("tips", "应聘者于：" + formatter.format(duserResume.getChangeDate()) + " 对简历进行了更新");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Do/UserResume/view";
    }
    //endregion


    //region 查看投递的简历
    @RequestMapping("/UserResume/ViewResumeCardno")
    public String ViewByCard(HttpServletRequest request, Model model) {
        try {
            String cardno = request.getParameter("cardno").trim();//身份证号
            UserResume resume = doResumeService.getUserResume(cardno);
            model.addAttribute("talent", "talent");

            if (resume == null) {
                model.addAttribute("valid", "no");
            } else {
                model.addAttribute("valid", "yes");
                model.addAttribute("cardno", cardno);
                model.addAttribute("fpath", utilService.getConfig("path"));
                model.addAttribute("pathfile", "/resources/ResumeAppends" + resume.getName() + resume.getCardno());
                model.addAttribute("isornot", EnumTools.IsOrNotList());

                List<UserAppend> userAppends = doResumeService.getUserAppend(cardno); //证明材料

                List<UserAppend> userAppendRys = new ArrayList<>();//荣誉
                List<UserAppend> userAppendJxs = new ArrayList<>();//奖项
                List<UserAppend> userAppendZcs = new ArrayList<>();//职称
                List<UserAppend> userAppendOts = new ArrayList<>();//其他

                for (UserAppend userAppend : userAppends) {
                    if (userAppend.getType().equals("G")) {
                        userAppend.setRealname("获得的荣誉" + (userAppendRys.size() > 0 ? "(" + userAppendRys.size() + ")" : ""));
                        userAppendRys.add(userAppend);
                    } else if (userAppend.getType().equals("1")) {
                        userAppend.setRealname("学校期间所获奖项" + (userAppendJxs.size() > 0 ? "(" + userAppendJxs.size() + ")" : ""));
                        userAppendJxs.add(userAppend);
                    } else if (userAppend.getType().equals("4")) {
                        userAppend.setRealname("职称或职业资格" + (userAppendZcs.size() > 0 ? "(" + userAppendZcs.size() + ")" : ""));
                        userAppendZcs.add(userAppend);
                    } else if (userAppend.getType().equals("A")) {
                        userAppend.setRealname("身份证正面");
                        userAppendOts.add(userAppend);
                    } else if (userAppend.getType().equals("B")) {
                        userAppend.setRealname("身份证反面");
                        userAppendOts.add(userAppend);
                    } else if (userAppend.getType().equals("C")) {
                        userAppend.setRealname("全日制教育毕业证");
                        userAppendOts.add(userAppend);
                    } else if (userAppend.getType().equals("D")) {
                        userAppend.setRealname("全日制教育学位证");
                        userAppendOts.add(userAppend);
                    } else if (userAppend.getType().equals("E")) {
                        userAppend.setRealname("非全日制教育毕业证");
                        userAppendOts.add(userAppend);
                    } else if (userAppend.getType().equals("F")) {
                        userAppend.setRealname("非全日制教育学位证");
                        userAppendOts.add(userAppend);
                    }
                }
                model.addAttribute("userAppendRys", userAppendRys);
                model.addAttribute("userAppendJxs", userAppendJxs);
                model.addAttribute("userAppendZcs", userAppendZcs);
                model.addAttribute("userAppendOts", userAppendOts);

                model.addAttribute("userresume", resume);

                List<UserEducation> educations = resumeService.getUserEducation(resume.getId());//教育背景
                List<UserEducation> educationsFull = new ArrayList<>();
                List<UserEducation> educationsNot = new ArrayList<>();
                for (UserEducation dUserEducation : educations) {
                    if (dUserEducation.getType().equals(1)) {
                        educationsFull.add(dUserEducation);
                    } else {
                        educationsNot.add(dUserEducation);
                    }
                }
                model.addAttribute("educations", educations);
                model.addAttribute("educationsFull", educationsFull);
                model.addAttribute("eduFullLen", educationsFull.size() < 2 ? 2 : (educationsFull.size() + 1));
                model.addAttribute("eduFullL", educationsFull.size());
                model.addAttribute("educationsNot", educationsNot);
                model.addAttribute("eduNotLen", educationsNot.size() < 2 ? 2 : (educationsNot.size() + 1));
                model.addAttribute("eduNotL", educationsNot.size());

                Date dates = new Date();
                Date brith = resume.getBirthday();
                String ages = getTime(dates, brith);
                model.addAttribute("ages", ages);


            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Do/UserResume/view";
    }
    //endregion

    public static String getTime(Date currentTime, Date firstTime) {
        long diff = currentTime.getTime() - firstTime.getTime();//这样得到的差值是微秒级别
        Calendar currentTimes = dataToCalendar(currentTime);//当前系统时间转Calendar类型
        Calendar firstTimes = dataToCalendar(firstTime);//查询的数据时间转Calendar类型
        int year = currentTimes.get(Calendar.YEAR) - firstTimes.get(Calendar.YEAR);//获取年
        int month = currentTimes.get(Calendar.MONTH) - firstTimes.get(Calendar.MONTH);
        int day = currentTimes.get(Calendar.DAY_OF_MONTH) - firstTimes.get(Calendar.DAY_OF_MONTH);
        if (day < 0) {
            month -= 1;
            currentTimes.add(Calendar.MONTH, -1);
            day = day + currentTimes.getActualMaximum(Calendar.DAY_OF_MONTH);//获取日
        }
        if (month < 0) {
            month = (month + 12) % 12;//获取月
            year--;
        }
        long days = diff / (1000 * 60 * 60 * 24);
        long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);//获取时
        long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);//获取分钟
        long s = (diff / 1000 - days * 24 * 60 * 60 - hours * 60 * 60 - minutes * 60);//获取秒
        //   String CountTime=""+"year"+"年"+month+"月"+day+"天"+hours+"小时"+minutes+"分"+s+"秒";
        String CountTime = String.valueOf(year);
        return CountTime;
    }

    // Date类型转Calendar类型
    public static Calendar dataToCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    //数字匹配
    private static final Pattern NUM_PATTERN = Pattern.compile("[0-9]*");

    //region 查看注册的简历
    @RequestMapping("/UserResume/ViewResume")
    public String ViewResume(HttpServletRequest request, Model model) {
        try {
            String cardno = request.getParameter("cardno").trim();//身份证号
            UserResume resume = doResumeService.getUserResume(cardno);
            if (resume == null) {
                model.addAttribute("hasresume", "no");
            } else {
                model.addAttribute("hasresume", "yes");
                model.addAttribute("fpath", utilService.getConfig("path"));
                model.addAttribute("isornot", EnumTools.IsOrNotList());

                String rid = resume.getId();
                List<UserEducation> educations = doResumeService.getUserEducation1(rid);//教育背景
                List<UserWorkExperience> workexperiences = doResumeService.getUserWorkExperience1(rid); //工作经验
                List<UserTraining> trainings = doResumeService.getUserTraining1(rid); //培训经历
                List<UserRewardPunishment> rewardpunishments = doResumeService.getUserRewardPunishment1(rid); //获奖信息
                List<UserFamily> families = doResumeService.getUserFamily1(rid);//家庭成员
                List<UserMajor> majorList = doResumeService.getUserMajor1(rid); //专业技术职称


                educations.forEach(userEducation -> {
                    if (NUM_PATTERN.matcher(userEducation.getFulltime()).matches()) {
                        if ("0".equalsIgnoreCase(userEducation.getFulltime())) {
                            userEducation.setFulltime("全日制");
                        } else {
                            userEducation.setFulltime("非全日制");
                        }
                    }
                });

                model.addAttribute("userresume", resume);
                model.addAttribute("educations", educations);
                model.addAttribute("workexperiences", workexperiences);
                model.addAttribute("trainings", trainings);
                model.addAttribute("rewardpunishments", rewardpunishments);
                model.addAttribute("families", families);
                model.addAttribute("majors", majorList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Do/UserResume/viewresume";
    }
    //endregion

}
